package com.xbongbong.paas.business.rule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.help.SymbolHelp;
import com.xbongbong.paas.help.symbol.AbstractSymbolHandle;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.RegexUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.parent.condition.ConditionFieldHandler;
import com.xbongbong.parent.condition.ConditionFieldHandlerFactory;
import com.xbongbong.parent.condition.ProConditionHelp;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleConditionPojo;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.constant.BusinessConstant;
import com.xbongbong.pro.enums.businessrule.enums.RuleValueTypeEnum;
import com.xbongbong.pro.enums.errorcodes.BusinessRuleErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ProErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2020/09/29 9:40
 */
@Component
public class BusinessRuleConditionHelp {

    @Resource
    private ProConditionHelp proConditionHelp;
    @Resource
    private ConditionFieldHandlerFactory conditionFieldHandlerFactory;

    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessRuleConditionHelp.class);

    /**
     * 获取检索条件
     *
     * @param filterConditions     过滤条件
     * @param paasFormDataEntity   数据实体
     * @param businessRuleInfoPojo
     * @return
     */
    public BusinessRuleConditionPojo getConditionList(List<FilterConditionsPoJo> filterConditions, PaasFormDataEntity paasFormDataEntity,
                                                      BusinessRuleInfoPojo businessRuleInfoPojo) throws XbbException {
        BusinessRuleConditionPojo businessRuleConditionPojo = new BusinessRuleConditionPojo();
        if (Objects.isNull(paasFormDataEntity)) {
            return businessRuleConditionPojo;
        }
        // 遍历过滤条件，组装条件查询
        FieldAttrEntity dataIdFieldAttrEntity = proConditionHelp.getDataIdFieldAttr();
        Map<String, FieldAttrEntity> targetExplainMap = businessRuleInfoPojo.getTargetExplainMap();
        targetExplainMap.put(FieldTypeEnum.DATAID.getAlias(), dataIdFieldAttrEntity);
        Map<String, FieldAttrEntity> thisExplainMap = businessRuleInfoPojo.getThisExplainMap();
        thisExplainMap.put(FieldTypeEnum.DATAID.getAlias(), dataIdFieldAttrEntity);
        // 子表单搜索条件
        List<ConditionsEntityExt> conditionList = new ArrayList<>();
        // 子表单搜索条件
        List<ConditionsEntityExt> subConditionList = new ArrayList<>();

        for (FilterConditionsPoJo filterCondition : filterConditions) {
            String attr = filterCondition.getAttr();
            if (StringUtil.isEmpty(attr)) {
                continue;
            }
            Object object;
            Integer targetFieldType = filterCondition.getFieldType();
            List<String> targetAttrArray = ExplainUtil.getAttrByPoint(attr);
            String targetSubAttr = "";
            if (targetAttrArray.size() == 2) {
                attr = targetAttrArray.get(0);
                targetSubAttr = targetAttrArray.get(1);
            }
            // 获取动态值
            if (Objects.equals(filterCondition.getValueType(), RuleValueTypeEnum.DYNAMIC_VALUE.getType())) {
                String thisAttr = filterCondition.getValueAttr();
                if (Objects.isNull(targetFieldType) || StringUtil.isEmpty(thisAttr)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.THIS_EXPLAIN_NOT_EXIST);
                }
                List<String> thisAttrArray = ExplainUtil.getAttrByPoint(thisAttr);
                if (thisAttrArray.size() == 2) {
                    thisAttr = thisAttrArray.get(0);
                }
                FieldAttrEntity fieldAttrEntity = thisExplainMap.get(thisAttr);

                FieldAttrEntity targetField = targetExplainMap.get(attr);
                if (Objects.isNull(fieldAttrEntity) || Objects.isNull(targetField)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.THIS_EXPLAIN_NOT_EXIST);
                }
                if (ExplainUtil.isSubForm(targetField.getFieldType())) {
                    // 目标表单为子表单的字段时
                    List<? extends FieldAttrEntity> jsonArray = targetField.getSubForm().getItems();
                    Map<String, FieldAttrEntity> targetSubExplainMap = ExplainUtil.getExplainMapByList(jsonArray);
                    targetField = targetSubExplainMap.get(targetSubAttr);
                    targetFieldType = targetField.getFieldType();
                    object = getValueByData(fieldAttrEntity, targetField, paasFormDataEntity, businessRuleInfoPojo, thisExplainMap, filterCondition, thisAttrArray);
                    businessRuleConditionPojo.setAttr(attr);
                    setCondition(targetSubAttr, null, targetFieldType, filterCondition, object, subConditionList, paasFormDataEntity);
                } else {
                    object = getValueByData(fieldAttrEntity, targetField, paasFormDataEntity, businessRuleInfoPojo, thisExplainMap, filterCondition, thisAttrArray);
                    setCondition(attr, null, targetFieldType, filterCondition, object, conditionList, paasFormDataEntity);
                }
            } else {
                // 固定值
                FieldAttrEntity targetField = targetExplainMap.get(attr);
                if (Objects.isNull(targetField)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.THIS_EXPLAIN_NOT_EXIST);
                }
                if (ExplainUtil.isSubForm(targetField.getFieldType())) {
                    // 目标表单为子表单的字段时
                    List<? extends FieldAttrEntity> jsonArray = targetField.getSubForm().getItems();
                    Map<String, FieldAttrEntity> targetSubExplainMap = ExplainUtil.getExplainMapByList(jsonArray);
                    targetField = targetSubExplainMap.get(targetSubAttr);
                    targetFieldType = targetField.getFieldType();
                    ConditionFieldHandler conditionFieldHandler = conditionFieldHandlerFactory.getHandlerFixedValueByFieldType(targetFieldType);
                    object = conditionFieldHandler.handlerFixedValue(targetField, businessRuleInfoPojo, filterCondition);
                    businessRuleConditionPojo.setAttr(attr);
                    setCondition(targetSubAttr, null, targetFieldType, filterCondition, object, subConditionList, paasFormDataEntity);
                } else {
                    ConditionFieldHandler conditionFieldHandler = conditionFieldHandlerFactory.getHandlerFixedValueByFieldType(targetFieldType);
                    object = conditionFieldHandler.handlerFixedValue(targetField, businessRuleInfoPojo, filterCondition);
                    setCondition(attr, null, targetFieldType, filterCondition, object, conditionList, paasFormDataEntity);
                }
            }
        }
        businessRuleConditionPojo.setConditionList(conditionList);
        businessRuleConditionPojo.setSubConditionList(subConditionList);
        return businessRuleConditionPojo;
    }

    private void setCondition(String attr, String targetSubAttr, Integer targetFieldType, FilterConditionsPoJo filterCondition, Object object, List<ConditionsEntityExt> conditionList, PaasFormDataEntity paasFormDataEntity) {
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt(attr, targetSubAttr, targetFieldType, filterCondition.getSymbol());
        if (proConditionHelp.isEmpty(object, targetFieldType)) {
            if ((Objects.equals(FieldTypeEnum.DATETIME.getType(), targetFieldType) || Objects.equals(FieldTypeEnum.ADDRESS.getType(), targetFieldType))
                    && (Objects.equals(conditionsEntityExt.getSymbol(), ConditionEnum.EMPTY.getSymbol()) || Objects.equals(conditionsEntityExt.getSymbol(), ConditionEnum.NOEMPTY.getSymbol()))) {
                // 日期字段、地址字段的为空不为空
                conditionList.add(conditionsEntityExt);
                return;
            } else {
                String symbol = ConditionEnum.EMPTY.getSymbol();
                if (Objects.equals(conditionsEntityExt.getSymbol(), ConditionEnum.NOEQUAL.getSymbol())) {
                    symbol = ConditionEnum.NOEMPTY.getSymbol();
                }
                conditionsEntityExt.setSymbol(symbol);
                conditionList.add(conditionsEntityExt);
                return;
            }
        }
        if (Objects.equals(FieldTypeEnum.NUM.getType(), targetFieldType) && !RegexUtil.isNumber(object.toString()) && !(object instanceof List)) {
            // 当过滤条件为数字字段时，对比时仅需实际值相等即可，不需要匹配小数位数。
            // 必须是数字类型， 否则不执行正则
            return;
        }

        if (object instanceof List) {
            List<Object> list = JSON.parseArray(JSON.toJSONString(object), Object.class);
            conditionsEntityExt.setValue(list);
        } else if (Objects.equals(FieldTypeEnum.DATAID.getAlias(), filterCondition.getValueAttr())
                && Objects.nonNull(paasFormDataEntity.getId())) {
            conditionsEntityExt.setValue(Arrays.asList(paasFormDataEntity.getId()));
        } else {
            List<Object> list = new ArrayList<>();
            list.add(object);
            conditionsEntityExt.setValue(list);
        }

        conditionList.add(conditionsEntityExt);
    }

    /**
     * 根据当前的字段类型获取数据
     *
     * @param fieldAttrEntity      当前表单字段属性
     * @param targetField          目标表单字段属性
     * @param paasFormDataEntity   当前表单数据
     * @param businessRuleInfoPojo
     * @param thisExplainMap       本表单字段解释
     * @param filterCondition      过滤条件
     * @param thisAttrArray        子表单字段，格式为["subForm_1", "text_1"] 由格式subForm_1.text_1解析而来
     * @return
     * @throws XbbException
     */
    public Object getValueByData(FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, PaasFormDataEntity paasFormDataEntity,
                                 BusinessRuleInfoPojo businessRuleInfoPojo, Map<String, FieldAttrEntity> thisExplainMap, FilterConditionsPoJo filterCondition, List<String> thisAttrArray) throws XbbException {
        // 当前表单数据
        Integer targetFieldType = targetField.getFieldType();
        JSONObject data = paasFormDataEntity.getData();
        String thisAttr = fieldAttrEntity.getAttr();
        Object object;
        if (ExplainUtil.isSubForm(fieldAttrEntity.getFieldType())) {
            if (Objects.isNull(paasFormDataEntity.getSubFormData())) {
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261028);
            }
            String thisSubAttr = thisAttrArray.get(1);
            Set<Object> valueSet = new HashSet<>();
            // 本表单是子表单时
            Map<String, FieldAttrEntity> thisSubExplainMap;
            List<? extends FieldAttrEntity> thisSubForm = fieldAttrEntity.getSubForm().getItems();
            thisSubExplainMap = ExplainUtil.getExplainMapByList(thisSubForm);
            FieldAttrEntity subFieldAttrEntity = thisSubExplainMap.get(thisSubAttr);
            JSONArray valueArray = paasFormDataEntity.getSubFormData().getJSONArray(thisAttr);
            ConditionFieldHandler conditionFieldHandler;
            // 当前表单数据解析
            Object thisValue = getValueByFieldType(fieldAttrEntity, targetField, paasFormDataEntity, businessRuleInfoPojo, thisAttrArray, thisSubExplainMap);
            List<Object> thisValueList = JSON.parseArray(JSON.toJSONString(thisValue), Object.class);
            for (int i = 0; i < thisValueList.size(); i++) {
                thisValue = thisValueList.get(i);
                businessRuleInfoPojo.setObject(thisValue);
                conditionFieldHandler = conditionFieldHandlerFactory.getHandlerTargetFormByFieldType(targetFieldType);
                Object value = conditionFieldHandler.handlerTargetFormField(paasFormDataEntity, valueArray.getJSONObject(i), subFieldAttrEntity, targetField, businessRuleInfoPojo, filterCondition);
                if (Objects.equals(FieldTypeEnum.NUM.getType(), targetFieldType) && !(value instanceof Number) && !(value instanceof List)) {
                    // 当过滤条件为数字字段时，对比时仅需实际值相等即可，不需要匹配小数位数。
                    // 必须是数字类型， 否则不执行正则
                    return new ArrayList<>();
                }
                if (Objects.nonNull(value) && !(value instanceof List)) {
                    valueSet.add(value);
                } else if (value != null) {
                    valueSet.addAll(JSON.parseArray(JSON.toJSONString(value), Object.class));
                }
            }
            if (valueSet.size() > 1) {
                filterCondition.setSymbol(ConditionEnum.INCLUDE.getSymbol());
            }

            object = new ArrayList<>(valueSet);
        } else {
            // 本表单不是子表单时
            object = getValueByFieldType(fieldAttrEntity, targetField, paasFormDataEntity, businessRuleInfoPojo, thisAttrArray, thisExplainMap);
            businessRuleInfoPojo.setObject(object);
            ConditionFieldHandler conditionFieldHandler = conditionFieldHandlerFactory.getHandlerTargetFormByFieldType(targetFieldType);
            object = conditionFieldHandler.handlerTargetFormField(paasFormDataEntity, paasFormDataEntity.getData(), fieldAttrEntity, targetField, businessRuleInfoPojo, filterCondition);
        }
        return object;
    }


    /**
     * 校验前置条件是否匹配
     *
     * @param preConditionList
     * @param thisFormData
     * @param businessRuleInfoPojo
     * @return false : 不可执行当前这条业务规则； true：可以执行
     * @throws XbbException
     */
    public boolean validatePreCondition(List<FilterConditionsPoJo> preConditionList, PaasFormDataEntity thisFormData, BusinessRuleInfoPojo businessRuleInfoPojo) throws XbbException {
        if (CollectionsUtil.isEmpty(preConditionList)) {
            return true;
        }
        try {
            Map<String, FieldAttrEntity> thisExplainMap = businessRuleInfoPojo.getThisExplainMap();
            for (FilterConditionsPoJo item : preConditionList) {
                // 获取固定值的数据
                FieldAttrEntity fieldAttrEntity = thisExplainMap.get(item.getAttr());
                if (Objects.isNull(fieldAttrEntity)) {
                    continue;
                }
                ConditionFieldHandler conditionFieldHandler = conditionFieldHandlerFactory.handlerPreConditionByFieldType(fieldAttrEntity.getFieldType());
                Object object = conditionFieldHandler.handlerPreConditionValue(fieldAttrEntity, businessRuleInfoPojo, item);
                boolean result = SymbolHelp.handleSymbol(object, thisFormData, item, fieldAttrEntity);
                if (!result) {
                    return false;
                }
            }
        } catch (XbbException e) {
            return false;
        } catch (Exception e) {
            LOGGER.error("businessRuleConditionHelp.validatePreCondition 出错：", e);
            return false;
        }
        return true;
    }


    /**
     * 根据字段获取当前表单的数据以及子表单的数据
     *
     * @param fieldAttrEntity      当前表单字段
     * @param targetField          目标表单
     * @param paasFormDataEntity   当前数据
     * @param businessRuleInfoPojo
     * @param thisSubExplainMap    字段容器，也可以是子表单的容器
     * @return
     * @throws XbbException
     */
    private Object getValueByFieldType(FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, PaasFormDataEntity paasFormDataEntity,
                                       BusinessRuleInfoPojo businessRuleInfoPojo, List<String> thisAttrArray, Map<String, FieldAttrEntity> thisSubExplainMap) throws XbbException {
        Integer thisFieldType = fieldAttrEntity.getFieldType();
        JSONObject data = paasFormDataEntity.getData();
        // 本表单字段
        if (ExplainUtil.isSubForm(thisFieldType) && Objects.nonNull(paasFormDataEntity.getSubFormData())) {
            String thisAttr = thisAttrArray.get(0);
            String thisSubAttr = thisAttrArray.get(1);
            fieldAttrEntity = thisSubExplainMap.get(thisSubAttr);
            if (Objects.isNull(fieldAttrEntity)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.THIS_EXPLAIN_NOT_EXIST);
            }
            thisFieldType = fieldAttrEntity.getFieldType();
            List<Object> list = new ArrayList<>();
            JSONArray jsonArray = paasFormDataEntity.getSubFormData().getJSONArray(thisAttr);
            if (Objects.isNull(jsonArray)) {
                jsonArray = new JSONArray();
            }
            ConditionFieldHandler conditionFieldHandler;
            for (int i = 0; i < jsonArray.size(); i++) {
                conditionFieldHandler = conditionFieldHandlerFactory.getHandlerByFieldType(thisFieldType);
                Object value = conditionFieldHandler.handlerThisFormField(paasFormDataEntity, jsonArray.getJSONObject(i), fieldAttrEntity, targetField, businessRuleInfoPojo);
                list.add(value);
            }
            return list;
        } else {
            ConditionFieldHandler conditionFieldHandler = conditionFieldHandlerFactory.getHandlerByFieldType(thisFieldType);
            return conditionFieldHandler.handlerThisFormField(paasFormDataEntity, data, fieldAttrEntity, targetField, businessRuleInfoPojo);
        }
    }


    /**
     * @param preConditionList
     * @param thisFormData,
     * @param businessRuleInfoPojo
     * @param stageName
     * @return java.lang.String
     * @Description 阶段跳转前置校验
     * @author sunshaoxiang
     * @Date 13:38
     * @since
     **/
    public String validateStagePreCondition(List<FilterConditionsPoJo> preConditionList, PaasFormDataEntity thisFormData, BusinessRuleInfoPojo businessRuleInfoPojo, String stageName) throws XbbException {
        if (CollectionsUtil.isEmpty(preConditionList)) {
            return null;
        }
        try {
            StringBuilder validateResult = new StringBuilder();
            StringBuilder remindInfo = new StringBuilder();
            Map<String, FieldAttrEntity> thisExplainMap = businessRuleInfoPojo.getThisExplainMap();
            for (FilterConditionsPoJo item : preConditionList) {
                // 获取固定值的数据
                if(Objects.isNull(item.getFieldType())){
                    continue;
                }
                FieldAttrEntity fieldAttrEntity = thisExplainMap.get(item.getAttr());
                if (Objects.isNull(fieldAttrEntity)){
                    break;
                }
                ConditionFieldHandler conditionFieldHandler = conditionFieldHandlerFactory.handlerPreConditionByFieldType(fieldAttrEntity.getFieldType());

                Integer fieldType = fieldAttrEntity.getFieldType();
                Object object = conditionFieldHandler.handlerPreConditionValue(fieldAttrEntity, businessRuleInfoPojo, item);
                boolean result = SymbolHelp.handleSymbol(object, thisFormData, item, fieldAttrEntity);
                if (!result) {
                    remindInfo.append(fieldAttrEntity.getAttrName()).append(I18nMessageUtil.getMessage(MessageConstant.FIELD_HAS_TO_SATISFY));
                    remindInfo.append(ConditionEnum.getConditionEnum(item.getSymbol()).getMemo());
                    //为空条件
                    if (Objects.isNull(object) || Objects.equals(ConditionEnum.EMPTY.getSymbol(), item.getSymbol()) || Objects.equals(ConditionEnum.NOEMPTY.getSymbol(), item.getSymbol())) {
                        remindInfo.append(BasicConstant.COMMA);
                    } else if (Objects.equals(FieldTypeEnum.ADDTIME.getType(), fieldType) || Objects.equals(FieldTypeEnum.UPDATETIME.getType(), fieldType) || Objects.equals(FieldTypeEnum.DATETIME.getType(), fieldType)) {
                        //日期集合
                        String dateType = fieldAttrEntity.getDateType();
                        if (object instanceof List) {
                            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(object));
                            for (int i = 0; i < jsonArray.size(); i++) {
                                Long date = new Long(jsonArray.get(i).toString());
                                if (Objects.equals(dateType, DateTimeEnum.TIME.getType())) {
                                    remindInfo.append(DateTimeUtil.getDateStringEpochSecond(date)).append(BasicConstant.COMMA);
                                } else {
                                    remindInfo.append(DateTimeUtil.getStringEpochSecond(date)).append(BasicConstant.COMMA);
                                }
                            }
                        } else {
                            if (Objects.equals(dateType, DateTimeEnum.TIME.getType())) {
                                remindInfo.append(DateTimeUtil.getDateStringEpochSecond((Long) object)).append(BasicConstant.COMMA);
                            } else {
                                remindInfo.append(DateTimeUtil.getStringEpochSecond((Long) object)).append(BasicConstant.COMMA);
                            }
                        }
                        //涉及关联条件
                    } else if (Objects.equals(FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), fieldType) || Objects.equals(FieldTypeEnum.USER.getType(), fieldType)
                            || Objects.equals(FieldTypeEnum.USER_GROUP.getType(), fieldType) || Objects.equals(FieldTypeEnum.DEPT.getType(), fieldType)
                            || Objects.equals(FieldTypeEnum.DEPT_GROUP.getType(), fieldType) || Objects.equals(FieldTypeEnum.DEPARTMENTID.getType(), fieldType)
                            || Objects.equals(FieldTypeEnum.CREATORID.getType(), fieldType)) {
                        JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(item.getValue()));
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            String name = jsonObject.getString(BusinessConstant.NAME);
                            remindInfo.append(name).append(BasicConstant.COMMA);
                        }
                        //地址条件
                    } else if (Objects.equals(FieldTypeEnum.ADDRESS.getType(), fieldAttrEntity.getFieldType())) {
                        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
                        Integer showDetailAddress = fieldAttrEntity.getShowDetailAddress();
                        if (showDetailAddress == 1) {
                            remindInfo.append(jsonObject.getString(BusinessConstant.PROVINCE)).append(jsonObject.getString(BusinessConstant.CITY)).append(jsonObject.getString(BusinessConstant.DISTRICT)).append(jsonObject.getString(BusinessConstant.ADDRESS)).append(BasicConstant.COMMA);
                        } else {
                            remindInfo.append(jsonObject.getString(BusinessConstant.PROVINCE)).append(jsonObject.getString(BusinessConstant.CITY)).append(jsonObject.getString(BusinessConstant.DISTRICT)).append(BasicConstant.COMMA);
                        }
                        //下拉框条件
                    } else if (Objects.equals(FieldTypeEnum.COMBO.getType(), fieldType)) {
                        if (object instanceof List) {
                            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(item.getValue()));
                            for (int i = 0; i < jsonArray.size(); i++) {
                                remindInfo.append(jsonArray.get(i)).append(BasicConstant.COMMA);
                            }
                        } else {
                            remindInfo.append(item.getValue()).append(BasicConstant.COMMA);
                        }
                    } else if (Objects.equals(FieldTypeEnum.RADIO_GROUP.getType(), fieldType)) {
                        if (object instanceof List) {
                            JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(item.getValue()));
                            for (int i = 0; i < jsonArray.size(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                String name = jsonObject.getString(BusinessConstant.Text);
                                remindInfo.append(name).append(BasicConstant.COMMA);
                            }
                        } else {
                            if (item.getValue() instanceof String){
                                remindInfo.append(item.getValue()).append(BasicConstant.COMMA);
                            }else {
                                ItemPoJo itemPoJo = JSONObject.parseObject(JSON.toJSONString(item.getValue()), ItemPoJo.class);
                                remindInfo.append(itemPoJo.getText()).append(BasicConstant.COMMA);
                            }
                        }
                    } else if (Objects.equals(FieldTypeEnum.CHECKBOX_GROUP.getType(), fieldType)) {
                        JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(item.getValue()));
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            String name = jsonObject.getString(BusinessConstant.Text);
                            remindInfo.append(name).append(BasicConstant.COMMA);
                        }
                        //集合条件
                    } else if (object instanceof List) {
                        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(item.getValue()));
                        for (int i = 0; i < jsonArray.size(); i++) {
                            remindInfo.append(jsonArray.get(i)).append(BasicConstant.COMMA);
                        }
                    } else {
                        remindInfo.append(object).append(BasicConstant.COMMA);
                    }
                }
            }
            if (StringUtil.isNotEmpty(remindInfo.toString())) {
                validateResult.append(remindInfo.toString()).append(String.format(I18nMessageUtil.getMessage(MessageConstant.UNABLE_TO_JUMP_STAGE), stageName));
            }
            return validateResult.toString();
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("businessRuleConditionHelp.validatePreCondition 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    /**
     * @param preConditionList
     * @param explainMap
     * @return void
     * @Description 校验阶段前置跳转的条件内容
     * @author sunshaoxiang
     * @Date 9:16
     * @since
     **/
    public static void validateStagePreCondition(List<FilterConditionsPoJo> preConditionList, Map<String, FieldAttrEntity> explainMap, String titleMsg) throws XbbException {
        if (CollectionsUtil.isEmpty(preConditionList)) {
            return;
        }
        for (FilterConditionsPoJo item : preConditionList) {
            String symbol = item.getSymbol();
            if (Objects.equals(ConditionEnum.EMPTY.getSymbol(), symbol) || Objects.equals(ConditionEnum.NOEMPTY.getSymbol(), symbol)) {
                continue;
            }
            FieldAttrEntity fieldAttrEntity = explainMap.get(item.getAttr());
            if (Objects.isNull(fieldAttrEntity)){
                return;
            }
            Object value = item.getValue();
            if (Objects.isNull(value)) {
                throw new XbbException(ProErrorCodeEnum.API_ERROR_260047, String.format(ProErrorCodeEnum.API_ERROR_260047.getMsg(), titleMsg, fieldAttrEntity.getAttrName()));
            } else if (Objects.equals(ConditionEnum.RANGE.getSymbol(), item.getSymbol())) {
                JSONArray jsonObject = JSONObject.parseArray(JSON.toJSONString(value));
                if (Objects.isNull(jsonObject.get(0)) || Objects.isNull(jsonObject.get(1))) {
                    throw new XbbException(ProErrorCodeEnum.API_ERROR_260047, String.format(ProErrorCodeEnum.API_ERROR_260047.getMsg(), titleMsg, fieldAttrEntity.getAttrName()));
                }
            } else if (Objects.equals(FieldTypeEnum.ADDRESS.getType(), fieldAttrEntity.getFieldType())) {
                JSONObject targetJson = JSON.parseObject(JSON.toJSONString(value));
                String targetProvince = targetJson.getString(BusinessConstant.PROVINCE);
                if (StringUtil.isEmpty(targetProvince)) {
                    throw new XbbException(ProErrorCodeEnum.API_ERROR_260047, String.format(ProErrorCodeEnum.API_ERROR_260047.getMsg(), titleMsg, fieldAttrEntity.getAttrName()));
                }
            } else if (Objects.equals(FieldTypeEnum.RADIO_GROUP.getType(), fieldAttrEntity.getFieldType())) {
                if (value instanceof ItemPoJo || value instanceof Map) {
                    ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(value), ItemPoJo.class);
                    boolean otherIsEmpty = Objects.equals(1, itemPoJo.getIsOther()) && (Objects.isNull(itemPoJo.getValue()) || StringUtil.isEmpty(itemPoJo.getValue().toString()));
                    if (otherIsEmpty) {
                        throw new XbbException(ProErrorCodeEnum.API_ERROR_260047, String.format(ProErrorCodeEnum.API_ERROR_260047.getMsg(), titleMsg, fieldAttrEntity.getAttrName()));
                    }
                } else if (value instanceof List) {
                    List<ItemPoJo> itemList = JSON.parseArray(JSON.toJSONString(value), ItemPoJo.class);
                    for (ItemPoJo itemPoJo : itemList) {
                        boolean otherIsEmpty = Objects.equals(1, itemPoJo.getIsOther()) && (Objects.isNull(itemPoJo.getValue()) || StringUtil.isEmpty(itemPoJo.getValue().toString()));
                        if (otherIsEmpty) {
                            throw new XbbException(ProErrorCodeEnum.API_ERROR_260047, String.format(ProErrorCodeEnum.API_ERROR_260047.getMsg(), titleMsg, fieldAttrEntity.getAttrName()));
                        }
                    }
                }

            }
            boolean flag = AbstractSymbolHandle.noEmpty(value);
            if (!flag) {
                throw new XbbException(ProErrorCodeEnum.API_ERROR_260047, String.format(ProErrorCodeEnum.API_ERROR_260047.getMsg(), titleMsg, fieldAttrEntity.getAttrName()));
            }
        }
    }
}
